/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.unpack.file.utils;

import cn.ac.iie.unpack.file.global.GlobalParas;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author gjw
 */
public class FileUntarAndMoveThread implements Runnable {

    public static final Logger LOG = Logger.getLogger(FileUntarAndMoveThread.class);

    File outputDir;

    public FileUntarAndMoveThread(File outputDir) {
        this.outputDir = outputDir;
    }

    @Override
    public void run() {
        FileUntarAndMove fileUntar = new FileUntarAndMove();
        File untaredFile = null;
        File tarFile = null;
        while (true) {
            LOG.info("file queue num: "+GlobalParas.FILE_QUEUE.size());
            try {
                String fileName = GlobalParas.FILE_QUEUE.poll(3, TimeUnit.SECONDS);
                
                if(fileName == null){
                    Thread.sleep(5000);
                    continue;
                }
                LOG.info("read 1 file. name: " + fileName);
                tarFile = new File(fileName);
            } catch (InterruptedException ex) {
                LOG.error(ExceptionUtils.getFullStackTrace(ex));
            }
            //untar
            if (tarFile != null) {
                try {
                    LOG.info("start untar");
                    untaredFile = fileUntar.unTar(tarFile, outputDir);
                } catch (ArchiveException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                } catch (IOException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                }
            }

            //copy and move
            if (untaredFile != null) {
                try {
                    fileUntar.copy(untaredFile, GlobalParas.DISTRI_DESTINATION_DIR);
                } catch (IOException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                }
                try {
                    fileUntar.move(tarFile, GlobalParas.SUS_UNTAR_DIR);
                } catch (IOException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                }
                try {
                    fileUntar.delete(new File(tarFile.getAbsoluteFile() + GlobalParas.FILE_SUFFIX), GlobalParas.SUS_UNTAR_DIR);
                } catch (IOException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                }
                try {
                    fileUntar.createOkFile(untaredFile,GlobalParas.DISTRI_DESTINATION_DIR);
                    fileUntar.createOkFile(untaredFile,GlobalParas.PARSER_DESTINATION_DIR);
                } catch (Exception ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                }
            }
        }

    }

}
